const express = require('express');

const app = express();

app.use(express.static(__dirname));

const whiteList = [
  'http://localhost:3000',
  'http://localhost:8081'
];
// 中间件
app.use(function(req, res, next) {
   const origin = req.headers.origin;
   if (whiteList.includes(origin)) {
     // 设置哪个源可以访问
     res.setHeader('Access-Control-Allow-Origin', origin)
     // res.setHeader('Access-Control-Allow-Headers', 'name')
     res.setHeader('Access-Control-Allow-Credentials', true)
     // res.setHeader('Access-Control-Expose-Headers', 'name'); // 允许获取的响应头
   }
   next();
})

app.get('/say', function(req, res) {
  const {pwd, cb} = req.query;

  res.send(
    `${cb}("我也${pwd}")`
  )
})


app.get('/plan', function (req, res){
    res.send({
        status: true,
        data: [
            {
                _id: '1',
                date: new Date().toDateString(),
                time: 2,
                content: 'this is first'
            },
            {
                _id: '2',
                date: new Date().toDateString(),
                time: 2,
                content: 'this is sec'
            }
        ]
    })
})


app.get('/getData', function(req, res) {
  console.log(req.headers);
  res.end('我不爱你')
})


app.listen(4000, function() {
  console.log('sever is started');
})